package org.liuh.designs.spider;

import lombok.NoArgsConstructor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.liuh.designs.spider.dtos.AddressNode;
import org.liuh.designs.spider.utils.HttpClientUtil;
import org.liuh.designs.spider.utils.SpiderHttpUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName: AreaSpiderTask
 * @Description: 省并行处理..
 * @Author: liuh
 * @Date: 2020/9/30 11:18
 * @Version: 1.0
 */
@NoArgsConstructor
public class AreaSpiderProvinceTask implements Runnable {

    private CountDownLatch countDownLatch;
    private AddressNode provinceNode;
    private AreaConstant.HttpUtilEnum utilEnum;

    public AreaSpiderProvinceTask(AddressNode provinceNode, CountDownLatch countDownLatch, AreaConstant.HttpUtilEnum utilEnum) {
        this.provinceNode = provinceNode;
        this.countDownLatch = countDownLatch;
        this.utilEnum = utilEnum;
    }

    @Override
    public void run() {
        String tmpUrl = provinceNode.getNodeUrl();
        String sourceUrl = provinceNode.getParentNode().getNodeUrl();

        String tmpCity = null;
        if (utilEnum == AreaConstant.HttpUtilEnum.NORMAL) {
            tmpCity = SpiderHttpUtils.sendGet(false, tmpUrl, null, null, AreaConstant.CHAR_SET);
        } else if (utilEnum == AreaConstant.HttpUtilEnum.HTTP_CLIENT) {
            tmpCity = HttpClientUtil.doHttpGet(tmpUrl, sourceUrl, null, AreaConstant.CHAR_SET);
        }
        if (tmpCity == null) {
            return;
        }
        Document cityPage = Jsoup.parse(tmpCity);
        if (cityPage == null) {
            return;
        }
        Elements cityTable = cityPage.getElementsByClass(AreaConstant.getNameByClass(AreaConstant.ClassEnum.city, AreaConstant.TagEnum.table));
        if (cityTable == null || cityTable.isEmpty()) {
            return;
        }
        Elements cityTrs = cityTable.first().getElementsByClass(AreaConstant.getNameByClass(AreaConstant.ClassEnum.city, AreaConstant.TagEnum.tr));
        if (cityTrs == null || cityTrs.isEmpty()) {
            return;
        }
        List<AddressNode> cityNodes = new ArrayList<>();
        for (Element cityTr : cityTrs) {
            Elements tdTags = cityTr.getElementsByTag(AreaConstant.TagEnum.td.name());
            if (tdTags == null) {
                continue;
            }
            //code
            Element element = tdTags.get(0);
            String code = element.text();
            if (code == null || code.isEmpty()) {
                continue;
            }
            Elements cityLinkA = element.getElementsByTag("a");
            String cityHref = cityLinkA.attr(AreaConstant.AttrEnum.href.name());
            //name
            String name = tdTags.get(1).text();
            AddressNode cityNode = new AddressNode();
            cityNode.setParentNode(provinceNode);
            cityNode.setAddressCode(code);
            cityNode.setLevel(AreaConstant.LevelEnum.LEVEL_2.getLevel());
            cityNode.setAddressName(name);
            cityNode.setNodeUrl(AreaConstant.getFullLinkUrl(AreaConstant.HOST, cityHref, AreaConstant.ClassEnum.city));
            cityNodes.add(cityNode);
        }
        System.out.println(String.format("%s, 虚拟市区数量%s， 实际市区数量：%s", provinceNode.getAddressName(), cityTrs.size(), cityNodes.size()));
        // 为省设置市..
        provinceNode.setChildNodeList(cityNodes);
        countDownLatch.countDown();
    }

}
